//
//  WelcomeViewController.swift
//  我的微博练习
//
//  Created by zhangguo on 15/10/11.
//  Copyright © 2015年 zhangguo. All rights reserved.
//

import UIKit
import SDWebImage

class WelcomeViewController: UIViewController {
    //图像底部约束
    private var iconBottomCons: NSLayoutConstraint?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
       privateUI()
        
        //加载用户头像
        if let urlstring = UserAccount.shareAccount?.avatar_large {
            //geng新头像
            iconView.sd_setImageWithURL(NSURL(string: urlstring)!)
            
        }
    }
    override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)
//        
        iconBottomCons?.constant = -UIScreen.mainScreen().bounds.height - iconBottomCons!.constant
        
        //动画
        UIView.animateWithDuration(1.2, delay: 0.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5.0, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            //强制更新约束
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
                
            //发送通知，切换控制器
            NSNotificationCenter.defaultCenter().postNotificationName(GFRootViewCotrollerSwitchNotification, object: true)
        }
    }
    
    //准备界面
    func privateUI() {
        view.addSubview(backImageview)
        view.addSubview(iconView)
        view.addSubview(Label)
        
        //自动布局
        //背景图片
        backImageview.ff_Fill(view)
        
        //头像
       let cons = iconView.ff_AlignInner(type: ff_AlignType.BottomCenter, referView: view, size: CGSize(width: 85, height: 85), offset: CGPoint(x: 0, y: -160))
        //记录底边约束
        iconBottomCons = iconView.ff_Constraint(cons, attribute: NSLayoutAttribute.Bottom)
        
        //文字
        Label.ff_AlignHorizontal(type: ff_AlignType.BottomCenter, referView: iconView, size: nil, offset: CGPointMake(0, 15))
        }

//加载背景图片
    private lazy var backImageview:UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    //头像
    private lazy var iconView:UIImageView = {
        let iv = UIImageView(image: UIImage(named: "avatar_default_big"))
        iv.layer.masksToBounds = true
        iv.layer.cornerRadius = 45
        
        return iv
    }()
    
    //文字
    private lazy var Label:UILabel = {
        let label = UILabel()
        label.text = "欢迎归来"
        label.sizeToFit()
        
        return label
    }()
}
